package com.face.console.tools.config;

import com.face.console.service.impl.UserOAuthServiceIpml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liudehua
 * @explain 授权服务器
 * @date 2021/12/18 23:49
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {


    private PasswordEncoder passwordEncoder;
    private AuthenticationManager authenticationManager;
    private UserOAuthServiceIpml userOAuthServiceIpml;
    private TokenStore tokenStore;
    private JwtAccessTokenConverter jwtAccessTokenConverter;
    private JwtTokenEnhancer jwtTokenEnhancer;

    @Autowired
    public AuthorizationServerConfig(PasswordEncoder passwordEncoder,
                                     AuthenticationManager authenticationManager,
                                     UserOAuthServiceIpml userOAuthServiceIpml,
                                     TokenStore tokenStore, JwtAccessTokenConverter jwtAccessTokenConverter, JwtTokenEnhancer jwtTokenEnhancer) {
        this.passwordEncoder = passwordEncoder;
        this.authenticationManager = authenticationManager;
        this.userOAuthServiceIpml = userOAuthServiceIpml;
        this.tokenStore = tokenStore;
        this.jwtAccessTokenConverter = jwtAccessTokenConverter;
        this.jwtTokenEnhancer = jwtTokenEnhancer;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        String finalSecret = passwordEncoder.encode("123456");
        //配置client_id
        clients.inMemory()
                //配置client_id
                .withClient("client_id")
                //配置client-secret
                .secret(finalSecret)
                //配置token有效期
                .accessTokenValiditySeconds(1000 * 3)
                //刷新token的有效期
                .refreshTokenValiditySeconds(1000 * 60)
                //配置redirect_uri，用于授权成功后跳转
//                .redirectUris("http://www.baidu.com")
                .redirectUris("http:/localhost:8083/face/api/user/login")
                //配置申请的权限范围
                .scopes("all")
                //配置grant_type，表示授权类型
                .authorizedGrantTypes("password","refresh_token")
        ;

    }

    /**
     * 密码模式需要配置
     *
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//        在认证服务器配置中配置JWT的内容增强器
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> delegates = new ArrayList<>();
        delegates.add(jwtTokenEnhancer);
        delegates.add(jwtAccessTokenConverter);
        enhancerChain.setTokenEnhancers(delegates);

        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userOAuthServiceIpml)
                .tokenStore(tokenStore)//配置token
                .accessTokenConverter(jwtAccessTokenConverter)//jwt模式
                .tokenEnhancer(enhancerChain);//配置到oauth


    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
// 获取密钥需要身份认证，使用单点登录时必须配置
        security.tokenKeyAccess("isAuthenticated()");
    }

}
